﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Server.Models;
using Server.Share;

namespace Server.Controllers.Manage
{
    [RoutePrefix("51API/UserManage")]
    public class UserManageController : ApiController
    {
        MShare comm = new MShare();
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("UserAdd")]
        public IHttpActionResult PostUserAdd(UserInfo userInfo, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.CheckAuth(comm.manager.id, "User001");
                comm.userCtrl.UserAdd(userInfo);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "UserManage001", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("UserEdit")]
        public IHttpActionResult PostUserEdit(UserInfo userInfo, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.CheckAuth(comm.manager.id, "User002");
                comm.userCtrl.UserEdit(userInfo);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "UserManage002", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 用户启用/禁用/到期
        /// </summary>
        /// <param name="userInfo"></param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("UserState")]
        public IHttpActionResult PostUserState(UserInfo userInfo, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.CheckAuth(comm.manager.id, "User003");
                comm.userCtrl.UserState(userInfo);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "UserManage003", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 通过ID获取用户信息
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("GetUserByID")]
        public IHttpActionResult PostGetUserByID(int id, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                //comm.CheckAuth(comm.manager.id, "");
                UserInfo ui = comm.userCtrl.GetUserByID(id);
                RetModel ret = new RetModel
                {
                    Data = ui,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "UserManage004", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("GetUserList")]
        public IHttpActionResult PostGetUserList(string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                //comm.CheckAuth(comm.manager.id, "");
                UserInfo[] ui = comm.userCtrl.GetUserList();
                RetModel ret = new RetModel
                {
                    Data = ui,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "UserManage005", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("DeviceAdd")]
        public IHttpActionResult PostDeviceAdd(DeviceInfoModel dev, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.CheckAuth(comm.manager.id, "Device001");
                comm.userCtrl.DeviceAdd(dev);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "UserManage006", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("DeviceDel")]
        public IHttpActionResult PostDeviceDel(int id, string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                comm.CheckAuth(comm.manager.id, "Device002");
                comm.userCtrl.DeviceDel(id);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "UserManage006", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 获取设备
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="Token"></param>
        /// <returns></returns>
        [Route("GetDeviceList")]
        public IHttpActionResult PostGetDeviceList(string Token)
        {
            try
            {
                comm.CheckInfo(Token);
                //comm.CheckAuth(comm.manager.id, "");
                comm.userCtrl.GetDeviceList();
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "UserManage006", ErrInfo = ex.Message, Data = null });
            }
        }
    }
}